package com.cxrj.zhsq.payManage.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.housePropertyManage.dao.PropertyHouseMapper;
import com.cxrj.zhsq.housePropertyManage.entity.PropertyHouse;
import com.cxrj.zhsq.parkingLotManage.dao.PropertyParkingSpaceMapper;
import com.cxrj.zhsq.parkingLotManage.entity.PropertyParkingSpace;
import com.cxrj.zhsq.payManage.dao.PropertyBillMapper;
import com.cxrj.zhsq.payManage.entity.PropertyBill;
import com.cxrj.zhsq.payManage.service.PropertyBillService;
import com.cxrj.zhsq.utils.DateUtils;

@Service
public class PropertyBillServiceImpl implements PropertyBillService {

	@Resource
	private PropertyBillMapper propertyBillMapper;

	@Resource
	private PropertyHouseMapper propertyHouseMapper;

	@Resource
	private PropertyParkingSpaceMapper propertyParkingSpaceMapper;

	@Override
	public Map<String, Object> selectPropertyBill(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyBillMapper.selectByCount(sqlMap) + "";
		List<PropertyBill> list = propertyBillMapper.selectPropertyBillsPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyBill(PropertyBill propertyBill, HttpServletRequest request) throws ParseException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c1 = 0;
		Integer c2 = 0;
		propertyBill.setId(UUID.randomUUID().toString());
		propertyBill.setCreateTime(new Date());
		propertyBill.setStatus("1");
		propertyBill.setCommunityId(request.getSession().getAttribute("communityId").toString());
		if (propertyBill.getPayProject().equals("物业费")) {
			PropertyHouse propertyHouse = propertyHouseMapper.selectByPrimaryKey(propertyBill.getPayObjectId());
			propertyBill.setPayObjectName(propertyHouse.getNumberPeriods() + "期" + propertyHouse.getBuilding() + "栋" + propertyHouse.getUnit() + "单元" + propertyHouse.getDoorCard());
			propertyHouse.setS2(propertyBill.getS2Date());
			propertyBill.setOrderId(request.getSession().getAttribute("communityCode").toString() + "W" + new Date().getTime());
			c1 = propertyBillMapper.insertSelective(propertyBill);
			c2 = propertyHouseMapper.updateByPrimaryKeySelective(propertyHouse);
		}
		if (propertyBill.getPayProject().equals("车位费")) {
			PropertyParkingSpace propertyParkingSpace = propertyParkingSpaceMapper.selectByPrimaryKey(propertyBill.getPayObjectId());
			propertyParkingSpace.setTel(propertyBill.getTel());
			propertyParkingSpace.setLicensePlate(propertyBill.getLicensePlate());
			propertyParkingSpace.setEndTime(DateUtils.getnewDate(new Date(), propertyBill.getYue()));
			propertyParkingSpace.setState(propertyBill.getState());
			propertyBill.setPayObjectName(propertyParkingSpace.getRegionName() + propertyParkingSpace.getNumber());
			propertyBill.setOrderId(request.getSession().getAttribute("communityCode").toString() + "C" + new Date().getTime());
			if(propertyBill.getState().equals("0")) {
				propertyBill.setRemark("车位出租，出租到" + propertyParkingSpace.getEndTime());
			}else {
				propertyBill.setRemark("车位出售，出售到" + propertyParkingSpace.getEndTime());
			}
			c1 = propertyBillMapper.insertSelective(propertyBill);
			c2 = propertyParkingSpaceMapper.updateByPrimaryKeySelective(propertyParkingSpace);
		}
		resultMap.put("result", 0);
		if (c1 > 0 && c2 > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
